package com.yndsoft.jc.ui.memberpage.myorder;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseFragmentActivity;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.ui.memberpage.myorder.MyAdapter;
import com.yndsoft.jc.ui.net.HttpUtil;
import com.yndsoft.jc.widget.LazyViewPager;
import com.yndsoft.jc.widget.PagerSlidingTabStrip;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/6.
 */

public class MyOrderActivity extends FragmentActivity implements View.OnClickListener, PagerSlidingTabStrip.OnPagerTitleItemClickListener {

    RelativeLayout base_backBtn;
    TextView base_title;
    //    private TabLayout tab;
//    private ViewPager pager;
//    @Bind(R.id.ps_tabview)
    PagerSlidingTabStrip ps_tabview;
    //    @Bind(R.id.viewPager_tab)
    LazyViewPager viewPager_tab;
    private MyPagerAdapter myPagerAdapter;
    private List<String> list;
    HttpUtil mHttpUtil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        if (mHttpUtil == null) {
            mHttpUtil = HttpUtil.getInstance();
        }

        initView();
        initTabs();
        setViews();
        setListener();
    }

    private void initView() {
        base_backBtn = findViewById(R.id.base_backBtn);
        base_title = findViewById(R.id.base_title);
        ps_tabview = findViewById(R.id.ps_tabview);
        viewPager_tab = findViewById(R.id.viewPager_tab);
//        pager = findViewById(R.id.pager);
//        tab = findViewById(R.id.tab);

    }

    private void initTabs() {
        list = new ArrayList<>();
        list.add("全部");
        list.add("待付款");
        list.add("已完成");
        list.add("已评论");
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.my_order));
//        pager.setAdapter(new MyAdapter(getSupportFragmentManager(), list));
//        /*Tab与ViewPager绑定*/
//        tab.setupWithViewPager(pager);
//        reflex(tab);
        viewPager_tab.setOffscreenPageLimit(0);
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager_tab.setAdapter(myPagerAdapter);
        viewPager_tab.setCurrentItem(0);
        ps_tabview.setTextColor(Color.parseColor("#3c3c3c"));
        ps_tabview.setSelectedTextColor(Color.parseColor("#3c3c3c"));
        ps_tabview.setDividerColor(Color.parseColor("#FFFFFF"));
        ps_tabview.setUnderlineHeight(0);
        ps_tabview.setUnderlineColor(Color.parseColor("#EBEBEB"));
        ps_tabview.setViewPager(viewPager_tab);
        ps_tabview.setIndicatorColor(Color.parseColor("#FFFFFF"));
        ps_tabview.setIndicatorHeight(2);
        ps_tabview.setZoomMax(0.0f);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.category_nav);
        ps_tabview.setUnderBitmap(bmp);
//        ps_tabview.setUnderlineColor(R.color.mainColor);
        ps_tabview.setOnPagerTitleItemClickListener(this);
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
        }
    }

    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DensityUtil.dip2px(tabLayout.getContext(), 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHttpUtil = null;
    }

    @Override
    public void onSingleClickItem(int position) {

    }

    @Override
    public void onDoubleClickItem(int position) {

    }

    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position);
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public Fragment getItem(int position) {
            return WholeFragment.newInstance(position);
        }

    }
}
